package abq2.jic.jic.plugin.operate.activity;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.Date;
import java.util.EventObject;
import java.util.List;

public class ActivityConfirm extends AbstractFormPlugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        Object primaryKey = this.getView().getFormShowParameter().getCustomParam("activityId");
        if (primaryKey != null) {
            QFilter idFilter = new QFilter("id", QCP.equals,primaryKey);

            DynamicObject activitiy = BusinessDataServiceHelper.loadSingle("abq2_activity",
                    "id," + //内码
                            "name," + //活动名称
                            "abq2_admin," + //活动管理员
                            "abq2_telephone," + //管理员手机号码
                            "abq2_time_end1," + //报名结束时间
                            "abq2_place," + //活动地点
                            "abq2_method," + //考勤方式
                            "abq2_type," +  //活动类型
                            "abq2_limit," + //限报人数
                            "abq2_introduce," + //活动介绍
                            "abq2_integerfield," + //活动分
                            "abq2_integerfield1",  //已报人数
                    new QFilter[]{idFilter});
            this.getModel().setValue("abq2_activity",primaryKey);
            this.getModel().setValue("abq2_admin",Long.parseLong(activitiy.getString("abq2_admin.id")));
            this.getModel().setValue("abq2_admin_phone",activitiy.get("abq2_telephone"));
            this.getModel().setValue("abq2_time_end",activitiy.getDate("abq2_time_end1"));
            this.getModel().setValue("abq2_place",activitiy.getString("abq2_place"));
            this.getModel().setValue("abq2_introduce",activitiy.getString("abq2_introduce"));
            this.getModel().setValue("abq2_integerfield",activitiy.getString("abq2_integerfield"));
            int sum = activitiy.getInt("abq2_limit");
            int already = activitiy.getInt("abq2_integerfield1");
            int enableNum = sum - already; //剩余人数
            this.getModel().setValue("abq2_remain",enableNum);
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("btnok");
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control) evt.getSource();
        if (source.getKey().equals("btnok")){
            Object primaryKey = this.getView().getFormShowParameter().getCustomParam("activityId");
            QFilter idFilter = new QFilter("id", QCP.equals,primaryKey);
            DynamicObject activity = BusinessDataServiceHelper.loadSingle("abq2_activity",
                    "id," + //内码
                            "name," + //活动名称
                            "abq2_admin," + //活动管理员
                            "abq2_telephone," + //管理员手机号码
                            "abq2_time_end1," + //报名结束时间
                            "abq2_timebegin2," + //活动时间开始
                            "abq2_time_end2," + //活动时间结束
                            "abq2_place," + //活动地点
                            "abq2_method," + //考勤方式
                            "abq2_type," +  //活动类型
                            "abq2_limit," + //限报人数
                            "abq2_introduce," + //活动介绍
                            "abq2_integerfield," + //活动分
                            "abq2_integerfield1",  //已报人数
                    new QFilter[]{idFilter});

            if (activity == null){
                this.getView().showTipNotification("课程信息有误，请重新选择");
                return;
            }
            int sum = activity.getInt("abq2_limit");
            int already = activity.getInt("abq2_integerfield1");
            int enableNum = sum - already; //剩余人数
            if (enableNum < 1){
                this.getView().showTipNotification("名额已满，不能报名该活动");
                return;
            }
            int timeEnable = new Date().compareTo(activity.getDate("abq2_time_end1"));
            if (timeEnable == 1){
                this.getView().showTipNotification("该活动已结束报名");
                return;
            }

            DynamicObject[] myActivities = BusinessDataServiceHelper.load("abq2_my_activity",
                    "creator," +
                            "number,abq2_activity",
                    new QFilter[]{new QFilter("creator",QCP.equals, RequestContext.get().getCurrUserId())});
            boolean enable = false;
            for (DynamicObject myActivity : myActivities) {
                long id = myActivity.getLong("abq2_activity.id");
                if (id == (long) primaryKey){
                    enable = true;
                    break;
                }
            }

            if (enable){
                this.getView().showTipNotification("该活动你已经报过名了，不能重复报名");
                return;
            }

            DynamicObject my_activity = BusinessDataServiceHelper.newDynamicObject("abq2_my_activity");
            StringBuilder sb1 = new StringBuilder(); //生成一个单据编号
            sb1.append("MYACTIVITY-");
            for (int i = 1; i <= 10; i++) {
                int ascii = 48 + (int) (Math.random() * 9);
                char c = (char) ascii;
                sb1.append(c);
            }
            my_activity.set("number",sb1);
            my_activity.set("name",activity.getString("name"));
            my_activity.set("abq2_name",activity.getString("name"));
            my_activity.set("status","A");
            my_activity.set("enable",1);
            my_activity.set("createtime",new Date());
            my_activity.set("creator",RequestContext.get().getCurrUserId());
            my_activity.set("abq2_type",activity.getString("abq2_type"));
            my_activity.set("abq2_admin",activity.getLong("abq2_admin.id"));
            my_activity.set("abq2_telephone",activity.getString("abq2_telephone"));
            my_activity.set("abq2_method",activity.getString("abq2_method"));
            my_activity.set("abq2_place",activity.getString("abq2_place"));
            my_activity.set("abq2_timebegin2",activity.getDate("abq2_timebegin2"));
            my_activity.set("abq2_time_end2",activity.getDate("abq2_time_end2"));
            my_activity.set("abq2_integerfield",activity.getInt("abq2_integerfield"));
            my_activity.set("abq2_introduce",activity.getString("abq2_introduce"));
            my_activity.set("abq2_activity",primaryKey);

            OperationResult saveOperate =
                    SaveServiceHelper.saveOperate("abq2_my_activity",
                    new DynamicObject[]{my_activity}, null);
            List<Object> successPkIds = saveOperate.getSuccessPkIds();
            activity.set("abq2_integerfield1",activity.getInt("abq2_integerfield1") + 1);
            SaveServiceHelper.update(activity);

            System.out.println(successPkIds.get(0));
            this.getView().returnDataToParent(true);
            this.getView().close();
        }
    }
}